home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr47 / extmath.zip / EXTDIVB.S < prev    next >
Text File  |  1993-01-06  |  2KB  |  119 lines

  1. ; 64-bit x 64-bit unsigned divide
  2. ; Compact but stupid algorithm
  3. ;
  4. ; Tim Victor, December 28, 1992
  5. ;
  6. ; Callable from C as follows:
  7. ; int ExtDivB(dividend, divisor, quotient, remainder);
  8. ;   returns 1 for attempted divide-by-zero, 0 otherwise
  9. ;
  10.  
  11.         .model  small
  12.         .code
  13.         public _ExtDivB
  14. _ExtDivB proc near
  15.  
  16.         push bp         ; save caller's stack frame
  17.         mov  bp,sp      ; address stack frame of this call
  18.         push si
  19.         push di
  20.  
  21. ; point si to divisor, zero-test
  22.         mov  si,[bp+6]
  23.         mov  ax,[si]
  24.         or   ax,[si+2]
  25.         or   ax,[si+4]
  26.         or   ax,[si+6]
  27.  
  28. ;signal divide-by-zero, bail if error
  29.         mov  ax,1       ; no effect on flags
  30.         jz   exithere
  31.  
  32. ; point di to quotient storage, copy in dividend
  33.         mov  di,[bp+8]
  34.         mov  bx,[bp+4]
  35.  
  36.         mov  ax,[bx]
  37.         mov  [di],ax
  38.         mov  ax,[bx+2]
  39.         mov  [di+2],ax
  40.         mov  ax,[bx+4]
  41.         mov  [di+4],ax
  42.         mov  ax,[bx+6]
  43.         mov  [di+6],ax
  44.  
  45. ; clear 64-bit accumulator (bp:dx:bx:ax)
  46.         push bp
  47.         sub  ax,ax
  48.         mov  bx,ax
  49.         mov  dx,ax
  50.         mov  bp,ax
  51.  
  52. ; init counter for 64 loop iterations
  53.         mov  cx,64
  54.  
  55. divloop:
  56. ; shift high bit out of dividend
  57.         shl  word ptr [di],1
  58.         rcl  word ptr [di+2],1
  59.         rcl  word ptr [di+4],1
  60.         rcl  word ptr [di+6],1
  61.  
  62. ; shift bit into accumulator
  63.         rcl  ax,1               
  64.         rcl  bx,1
  65.         rcl  dx,1
  66.         rcl  bp,1
  67.  
  68. ; compare divisor to accum
  69.         cmp  bp, [si+6]
  70.         jb   nosub
  71.         ja   subdiv
  72.  
  73.         cmp  dx, [si+4]
  74.         jb   nosub
  75.         ja   subdiv
  76.  
  77.         cmp  bx, [si+2]
  78.         jb   nosub
  79.         ja   subdiv
  80.  
  81.         cmp  ax, [si]
  82.         jb   nosub
  83.  
  84. subdiv:
  85. ; if accum > divisor, subtract divisor
  86.         sub  ax, [si]
  87.         sbb  bx, [si+2]
  88.         sbb  dx, [si+4]
  89.         sbb  bp, [si+6]
  90.  
  91. ; put a one bit in quotient
  92.         inc  word ptr [di]
  93.  
  94. nosub:
  95.         loop divloop
  96.  
  97. ; done, store remainder
  98.         pop  si          ; get call frame addr
  99.         mov  si,[si+0Ah] ; addr of remainder
  100.         mov  [si],ax
  101.         mov  [si+2],bx
  102.         mov  [si+4],dx
  103.         mov  [si+6],bp
  104.  
  105. ; signal success
  106.         sub  ax,ax
  107.  
  108. ; restore caller's regs
  109. exithere:
  110.         pop  di
  111.         pop  si
  112.         pop  bp
  113.  
  114.         ret
  115.  
  116. _ExtDivB endp
  117.         end
  118.  
  119.